package sskj.regulatory.presentation.screen.launcher

import android.Manifest
import android.app.Activity
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import com.qmui.arch.QMUIFragment
import com.vondear.rxtools.RxPermissionsTool
import sskj.regulatory.R
import sskj.regulatory.arch.BackHandledInterface
import sskj.regulatory.presentation.base_mvp.base.BaseActivity
import sskj.regulatory.presentation.screen.login.LoginFragment
import javax.inject.Inject

class LauncherActivity : BaseActivity<LauncherContract.View, LauncherContract.Presenter>(), BackHandledInterface {

    private var mBackHandedFragment: QMUIFragment? = null

    override fun setSelectedFragment(selectedFragment: QMUIFragment?) {
        this.mBackHandedFragment = selectedFragment;
    }

    @Inject
    lateinit var mPresenter: LauncherPresenter

    override fun injectDependencies() {
        activityComponent.inject(this)
    }

    override fun initPresenter() = mPresenter


    override fun getContextViewId(): Int {
        return R.id.regulatory_luancher
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        isFullScreen(this)

        RxPermissionsTool.with(this).addPermission(Manifest.permission.ACCESS_FINE_LOCATION).addPermission(Manifest.permission.ACCESS_COARSE_LOCATION).addPermission(Manifest.permission.READ_EXTERNAL_STORAGE).addPermission(Manifest.permission.CAMERA).addPermission(Manifest.permission.CALL_PHONE).addPermission(Manifest.permission.READ_PHONE_STATE).initPermission()

        if (savedInstanceState == null) {
            val fragment = LauncherFragment()
            supportFragmentManager
                    .beginTransaction()
                    .add(contextViewId, fragment, fragment::class.java.simpleName)
                    .addToBackStack(fragment::class.java.simpleName)
                    .commit()
        }
    }

    private fun isFullScreen(activity: Activity): Boolean {
        return activity.window.attributes.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN == WindowManager.LayoutParams.FLAG_FULLSCREEN
    }


    override fun onBackPressed() {
        when (mBackHandedFragment?.onBackPressed()) {
            true -> {
                super.onBackPressed()
            }
        }
    }

}